package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.PrItemDTO;
import com.financing.wap.service.IPrItemService;

@Controller("prItemController")
@RequestMapping("/api/prItem")
public class PrItemController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IPrItemService prItemService;
	
	@RequestMapping("/listPrItem")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, PrItemDTO prItem,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query prItem list begin .......");
			
			prItem.setBegin(pager.getStartIndex());
			prItem.setPageSize(pager.getPageSize());
			String openid = request.getParameter("openid");
			prItem.setOpenId(openid);
			
			int count = prItemService.queryListCount(prItem);
			
			pager.setTotalCount(count);
			
			List<PrItemDTO> prItemList = null;
			
			if(count > 0){
				prItemList = prItemService.queryList(prItem);
			}else{
				prItemList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", prItemList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query prItem list end .......");
		}catch(Exception e){
			log.error("query prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	@RequestMapping("/addPrItem")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();

		try{
			logger.info("add prItem  begin .......");

			DTOdateSetUtil.setCreateInfo(prItem);
			DTOdateSetUtil.setUpdateInfo(prItem);

			int id = prItemService.insert(prItem);
			result.setAttribute("id", id);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add prItem list end .......");
		}catch(Exception e){
			log.error("add prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}
	@RequestMapping("/endPrItem")
	@ResponseBody
	public ResultMap endPrItem(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();

		try{
			logger.info("update prItem  begin .......");

			Integer id = prItem.getId();
			String openid = request.getParameter("openid");
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			//不是已领取，不能完成
			PrItemDTO prItemDTO = prItemService.loadById(id);
			if(!"1".equals(prItemDTO.getItemStatus())){
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			if(!openid.equals(prItemDTO.getOpenId())){
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(prItem);
			prItem.setItemStatus("2");
			prItem.setInvoiceStatus("0");
			prItem.setSettlementStatus("0");
			prItemService.update(prItem);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update prItem list end .......");
		}catch(Exception e){
			log.error("update prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

	@RequestMapping("/receivePrItem")
	@ResponseBody
	public ResultMap receivePrItem(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();

		try{
			logger.info("update prItem  begin .......");

			Integer id = prItem.getId();
			String openid = request.getParameter("openid");
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(prItem);
			prItem.setItemStatus("1");
			prItem.setOpenId(openid);

			prItemService.update(prItem);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update prItem list end .......");
		}catch(Exception e){
			log.error("update prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/statistics")
	@ResponseBody
	public ResultMap statistics(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();

		try{
			logger.info("query statistics begin .......");

			String openid = request.getParameter("openid");
			Map map = prItemService.queryStatistics(openid);
			if(map.get("flAmountAll") == null){
				map.put("flAmountAll",0);
				map.put("flAmountNow",0);
				map.put("flAmountMonth",0);
			}

			result.setAttribute("bean",map);

			List<PrItemDTO> prItemDTOs = prItemService.queryStatisticsList(openid);
			result.setAttribute("list",prItemDTOs);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);


			logger.info("query statistics list end .......");
		}catch(Exception e){
			log.error("update prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}
	
	@RequestMapping("/detailPrItem")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail prItem  begin .......");
			
			Integer id = prItem.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			PrItemDTO dto = prItemService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail prItem  end .......");
		}catch(Exception e){
			log.error("query detail prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deletePrItem")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete prItem  begin .......");
			
			Integer id = prItem.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			prItemService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete prItem  end .......");
		}catch(Exception e){
			log.error("delete prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updatePrItem")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update prItem  begin .......");
			
			Integer id = prItem.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prItem);
			
			prItemService.update(prItem);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update prItem list end .......");
		}catch(Exception e){
			log.error("update prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForcePrItem")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, PrItemDTO prItem){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce prItem  begin .......");
			
			Integer id = prItem.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prItem);
			
			prItemService.updateForce(prItem);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce prItem list end .......");
		}catch(Exception e){
			log.error("updateForce prItem error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
